const fs = require('fs');//?文件夹操作

let server = function (app, db) {

    // const web_information=fs.readFileSync('./html/webinformation.html');//?本站信息

    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from login where UserCookie=\'' + request.cookies.DOM_Cookie + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //? 浏览页响应
    app.get('/', function (request, response) {
        View_Cookie(request, function (err, data) {
            response.cookie('DOM_Cookie', request.cookies.DOM_Cookie, { maxAge: 60 * 60 * 1000 });
            fs.readFile('html/recipe.html', function (err, data_) {
                // console.log(data);
                if (data[0]) {
                    let html = data_.toString().replace(`<li class="nav-item"><a id="login" class="nav-link" href="./login">登录</a></li>`, `<li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
                    aria-expanded="false">
                    我的
                </a>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="./myselfs_part">个人中心</a></li>
                    <li><a class="dropdown-item" href="./myrelease" id="myrelease">我的发布</a></li>
                    <li><a class="dropdown-item" href="./html/insert" id="wantrelease">我要发布</a></li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    <li><a class="dropdown-item" href="#" id="Exit">退出登录</a></li>
                </ul>
            </li>`).replace('</div data-position>', fs.readFileSync('./html/webinformation.html') + '</div data-position>')
                    response.send(html)
                }
                else response.send(data_.toString())
            })
        })

    })

    //? 菜品添加页面
    app.get('/insert', function (request, response) {
        response.cookie('DOM_Cookie', request.cookies.DOM_Cookie, { maxAge: 60 * 60 * 1000 });
        fs.readFile('html/insert.html', function (err, data) {
            response.send(data.toString())
        })
    })

    //?个人中心页面
    app.get('/myselfs_part', function (request, response) {
        response.cookie('DOM_Cookie', request.cookies.DOM_Cookie, { maxAge: 60 * 60 * 1000 });
        fs.readFile('html/myselfs_part.html', function (err, data) {
            response.send(data.toString())
        })
    })

    //?个人发布页面
    app.get('/myrelease', function (request, response) {
        response.cookie('DOM_Cookie', request.cookies.DOM_Cookie, { maxAge: 60 * 60 * 1000 });

        fs.readFile('html/myrelease.html', function (err, data) {
                    response.send(data.toString().replace('</div data-position>',fs.readFileSync('./html/webinformation.html')+'</div data-position>')
)
        })
    })

    //?菜品详情页
    app.get('/details/:FoodID', function (request, response) {
        response.cookie('DOM_Cookie', request.cookies.DOM_Cookie, { maxAge: 60 * 60 * 1000 });
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('select * from recipe where FoodID=\'' + request.params.FoodID + '\' and UserID=' + data[0].UserID, function (err, data1) {
                    if (data1[0]) {
                        fs.readFile('html/details.html', function (err, data) {
                            response.send(data.toString()
                            .replace('<input type="" id="FoodID" style="display: none;" data-FoodID="">', '<input type="" id="FoodID" style="display: none;" data-FoodID="' + request.params.FoodID + '">')
                            .replace('</div data-position>',fs.readFileSync('./html/webinformation.html')+'</div data-position>')
                            
                            )
                        
                        })
                    } else {
                        fs.readFile('html/details.html', function (err, data) {
                            response.send(data.toString()
                            .replace('<li class="nav-item"><a class="nav-link" href="#">修改</a></li>', '')
                            .replace('<input type="" id="FoodID" style="display: none;" data-FoodID="">', '<input type="" id="FoodID" style="display: none;" data-FoodID="' + request.params.FoodID + '">')
                            .replace('</div data-position>',fs.readFileSync('./html/webinformation.html')+'</div data-position>')
                            )
                        })
                    }
                })
            } else {
                fs.readFile('html/details.html', function (err, data) {
                    response.send(data.toString()
                    .replace('<li class="nav-item"><a class="nav-link" href="#">修改</a></li>', '')
                    .replace('<input type="" id="FoodID" style="display: none;" data-FoodID="">', '<input type="" id="FoodID" style="display: none;" data-FoodID="' + request.params.FoodID + '">')
                    .replace('</div data-position>',fs.readFileSync('./html/webinformation.html')+'</div data-position>')
                    )
                })
            }
        })
    })

    //?登录页响应
    app.get('/login', function (request, response) {
        fs.readFile('html/login.html', function (err, data) {
            response.send(data.toString())
        })
    })

    //?页面请求
    app.get('/html/:op', function (request, response) {
        if (request.params.op == 'login') {
            response.redirect('/login')
        }
        else if (request.params.op == 'insert') {
            if (request.cookies.DOM_Cookie)
                response.redirect('/insert')
        } else if (request.params.op == 'recipe') response.redirect('/')
        else if (request.params.op == 'myselfs_part') response.redirect('/myselfs_part')
        else if (request.params.op == 'details') response.redirect('/details')
        else response.end()
    })
}
module.exports = server